********************************************************
*** Table 6: Predicted Effect of Tax Changes on Income

	import delimited using "${mypath}/intermediate_csv/choice_model_mean_spec_inc.csv", clear varnames(1) stringcols(1)
	keep nrmp_spec_desc nrmp_spec_code mean_hourly_income n
	foreach v of varlist mean_hourly_income nrmp_spec_code n{
		destring `v', replace
	}
	* Mean hourly income for dermatologists
	su mean_hourly_income if nrmp_spec_desc == "Dermatology" 
		local mhinc_derm: di %2.0f `r(mean)'
		di "`mhinc_derm'"
	su mean_hourly_income if nrmp_spec_desc == "Internal Medicine"
		local mhinc_im: di %2.0f `r(mean)'
	
	* Mean hourly income for specialists
	local keeplist Anesthesiology Dermatology ObGyn Orthopaedic Plastic Psychiatry Radiation Radiology Surgery
	gen flag = 0 
	foreach s in `keeplist'{
		replace flag = 1 if strpos(nrmp_spec_desc, "`s'")
	}
	keep if flag==1
	collapse (mean) mean_hourly_income [aw=n]
	replace mean_hourly_income = round(mean_hourly_income)
	local aveinc= `=mean_hourly_income[1]'
	
	clear
	local decreasederm = ln(`mhinc_derm' / `aveinc')
	local increaseim = ln(`aveinc' / `mhinc_im' )
	gen delta_y=.
	gen id = . 
	local spec 0 
	foreach epsilon in 0.44 1.00{
		capture gen epsilon=.
		foreach delta_y in 0.05 `decreasederm' `increaseim' {
			foreach taxrate in .37 .396{
				capture gen taxrate=.
				capture gen newtax=.
				foreach i of numlist 1 -1{
					local dely `i'*`delta_y'
					set obs `=_N+1'
					replace delta_y = `dely'    if mi(delta_y)
					replace epsilon = `epsilon' if mi(epsilon)
					replace taxrate = `taxrate' if mi(taxrate)
					if `i'==-1{
						replace id = `spec'+1 if mi(id)
						local ++spec
					}
				}
			}
		}
	}
	replace newtax = 1 - exp((delta_y/epsilon) + ln(1-taxrate))
	replace newtax = round(newtax, .01)
	replace newtax = newtax*100
	replace taxrate = taxrate*100
	* Format 
	foreach v of varlist taxrate newtax{
		tostring `v', format("%02.0f") replace force 
		replace `v' = `v' + "\%"
	}
	replace newtax = "1\%" if newtax == "01\%"
	foreach v of varlist delta_y epsilon{
		tostring `v', format("%04.2fc") replace force 
	}
	drop if strpos(taxrate, "40\%") 
	drop if ( strpos(delta_y, ".45") | strpos(delta_y, "0.22")) & epsilon == "0.44"
	replace id = 12 if id==9
	* Panels
	gen panel = "\bf Panel A: Baseline Empirical Elasticity"
	replace panel = "\bf Panel B: Augmented Elasticity per Scheuer and Werning (2017)" if id==7
	replace panel = "\bf Panel C: Internal Medicine vs. Average Specialist" if id==11
	replace panel = "\bf Panel D: Average Specialist vs. Dermatology" if id==12
	
	texdoc init "${tables}/tax_policy_table.tex", replace
	
	tex \begin{table}[h!] 
	tex \caption{\bf Predicted Effects of Tax Changes on Income \label{tab:taxpolicytable}} 
	tex \begin{center}
	tex \resizebox{0.8\textwidth}{!}{
	tex \begin{tabular}{cccc} 
	tex \midrule \midrule 
	tex \bf Elasticity & \bf Income Growth & \bf Original Tax Rate & \bf New Tax Rate \\ 
	tex \cmidrule(lr){1-1} \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4}
	tex $\epsilon$ & $\Delta y$ & $\tau_0$ & $\tau_1$ \\ \midrule \addlinespace[2ex]

	levelsof id
	local lastrow 1
	foreach i in `r(levels)'{
		if `i'>`lastrow'{
			local lastrow = `i'
		}
	}
	foreach i in `r(levels)'{
		preserve 
			keep if id==`i'
			gsort -delta_y 
				tex \multicolumn{4}{l}{`=panel[1]'} \\ 
				tex \addlinespace[1.25ex]
				tex `=epsilon[1]' & `=delta_y[1]' & `=taxrate[1]' & `=newtax[1]' \\
				tex `=epsilon[2]' & `=delta_y[2]' & `=taxrate[2]' & `=newtax[2]' \\ 
			if `i'<`lastrow'{
				tex \addlinespace[1.25ex]
			}
		restore 
	}
	tex \addlinespace[0.5ex]
	tex \midrule \midrule
	tex \end{tabular}
	tex }
	tex \end{center}
	tex \end{table}
	
	texdoc close	
